<script lang="ts" setup>
    import axios from 'axios';
    import { onMounted, ref } from 'vue';
    import { useRoute, useRouter } from 'vue-router';
    import type { BookDetail } from '@/type';

    const route = useRoute()
    const router = useRouter()
    let detailsData = ref<BookDetail>({
      id:0,
      name:'',
      type:'',
    } as BookDetail)

    onMounted(async () => {
        let res = await axios.get("http://localhost:8080/api/books/"+route.query.id,{
            headers:{
                "Authorization": "Bearer " + localStorage.getItem("token")
            }
        })
        detailsData.value = res.data
    })

    async function submitForm(){
      console.log(detailsData.value)
      const bookId = detailsData.value.id
      let res = await axios.put(`http://localhost:8080/api/books/${bookId}`,{
        name:detailsData.value.name,
        type:detailsData.value.type,
        headers:{
                "Authorization": "Bearer " + localStorage.getItem("token")
        }
      })
      
      router.push("/")
    }
    function resetForm(){}
</script>

<template>
   <h1>修改界面</h1>
    <el-form
    style="max-width: 600px"
    status-icon
    label-width="0px"
    class="demo-ruleForm"
  >
    <el-form-item>
        <el-input
          v-model="detailsData.id"
          style="width: 240px"
          disabled
          placeholder="Please input"
        /> <br/>
    </el-form-item>
    <el-form-item>
        <el-input v-model="detailsData.name" style="width: 240px" placeholder="Please input" /> <br/>
    </el-form-item>
    <el-form-item>
        <el-input v-model="detailsData.type" style="width: 240px" placeholder="Please input" /> <br/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm()">
        Submit
      </el-button>
      <el-button @click="resetForm()">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped>

</style>